<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24" style='width: 400px'>
            <a-form-item label="客户">
              <JSelectMultiUser v-model="queryParam.customerId" :query-config="selectUserQueryConfig" :url="kehuurl"
                                @change="kehuChange" />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('退货退款模块')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0" v-has="'refundreturn:delete'">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel" ><a-icon type="delete" />删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div>
        <a-tabs default-active-key="0" @change="tabsChange">
          <a-tab-pane key="0" tab="全部" force-render></a-tab-pane>
          <a-tab-pane key="3" tab="我负责的"></a-tab-pane>
          <a-tab-pane key="4" tab="我协同的"></a-tab-pane>
          <a-tab-pane key="5" tab="下属负责的"></a-tab-pane>
          <a-tab-pane key="6" tab="下属协同的"></a-tab-pane>
        </a-tabs>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <!--        产品相关字段-->
        <template slot="chanpinname" slot-scope="text,record">
          <div class="table-warp">
            {{ record.products.length > 0 ? record.products[0].name : '' }}
            <a-popover trigger="click" class="chakanquanbu">
              <template slot="content">
                <a-table class="chanpin-table" :columns="seecolumns" :data-source="record.products" size="middle"
                         bordered :scroll="{x:true}"
                         :pagination="false">
                </a-table>
              </template>
              <a-button type="link">
                查看全部
              </a-button>
            </a-popover>
          </div>
        </template>
                三元使用 为修正表格错位
        <template slot="chanpinnumber" slot-scope="text,record">
          <span>{{ record.products.length > 0 ? record.products[0].number : '' }}</span>
        </template>
        <template slot="specifications" slot-scope="text,record">
          <span>{{ record.products.length > 0 ? record.products[0].specifications : '' }}</span>
        </template>
        <template slot="unitText" slot-scope="text,record">
          <span>{{ record.products.length > 0 ? record.products[0].unitText : '' }}</span>
        </template>
        <template slot="num" slot-scope="text,record">
          <span>{{ record.products.length > 0 ? record.products[0].num : '' }}</span>
        </template>
        <template slot="unitPrice" slot-scope="text,record">
          <span>{{ record.products.length > 0 ? record.products[0].unitPrice : '' }}</span>
        </template>
        <template slot="quotedPriceTotal" slot-scope="text,record">
          <span>{{ record.products.length > 0 ? record.products[0].quotedPriceTotal : '' }}</span>
        </template>
        <template slot="remarks" slot-scope="text,record">
          <span>{{ record.products.length > 0 ? record.products[0].remarks : '' }}</span>
        </template>

        //退款退货详情
        <template slot="name" slot-scope="text, record">
          <a v-html="text" @click="handleDetail(record)" color="blue"></a>
        </template>
        <template slot="number" slot-scope="text, record">
          <a v-html="text" @click="handleDetail(record)" color="blue"></a>
        </template>
        //客户详情
        <template slot="customerIdDictText" slot-scope="text,record">
          <a @click="seeCustomerId(record)">{{ text }}</a>
        </template>
        //合同详情
        <template slot="orderContactsIdDictText" slot-scope="text,record">
          <a @click="seeAgreementId(record)">{{ text }}</a>
        </template>

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a  v-has="'refundreturn:delete'">删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>

      <div class="buttonStyle">
        <div class="ant-alert ant-alert-info">
          <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
          <a-divider class="btn" type="vertical" />
          <a  @click="onClearSelected">清空</a>
          <a-button  type="default" @click="MAction(false,'/refundreturn/refundReturn/updateCollaboratorIds',1,1260)" v-has="'refundreturn:transfer'" >移交</a-button>
          <a-button  type="default" @click="MAction(false,'/refundreturn/refundReturn/updateByDirectorIds',2,1260)">添加负责人</a-button>
          <a-button  type="default" @click="MAction(false,'/refundreturn/refundReturn/deleteUser',2,1260)">删除负责人</a-button>
          <a-button  type="default" @click="MAction(true,'/refundreturn/refundReturn/updateByDirectorIds',3,1260)">添加协同人</a-button>
          <a-button  type="default" @click="MAction(true,'/refundreturn/refundReturn/deleteUser',3,1260)">删除协同人</a-button>
        </div>
      </div>
    </div>
    <refund-return-modal ref="modalForm" @ok="modalFormOk"></refund-return-modal>
    <CustomerModal ref="CustomerModal"></CustomerModal>
    <ContractOrderModal ref="ContractOrderModal" ></ContractOrderModal>
    <JSelectUserByDepModalCustom ref="MAmodel" :modalWidth="MAwidth" :multi="MAisMulti" @ok="MACallback"></JSelectUserByDepModalCustom>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import RefundReturnModal from './modules/RefundReturnModal.Style#Drawer'
  import {filterMultiDictText} from '@/components/dict/JDictSelectUtil'
  import { getAction, postAction } from '@api/manage'
  import JSelectUserByDepModalCustom from '@comp/jeecgbiz/modal/JSelectUserByDepModalCustom'
  import CustomerModal from '@views/customer/modules/CustomerModal.Style#Drawer'
  import ContractOrderModal from '@views/ContractOrderList/modules/ContractOrderModal.Style#Drawer'
  import JSelectMultiUser from '@/components/jeecgbiz/JSelectMultiUserCustom'
  export default {
    name: 'RefundReturnList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      RefundReturnModal,
      JSelectMultiUser,
      JSelectUserByDepModalCustom,
      CustomerModal,
      ContractOrderModal,
    },
    data () {
      return {
        kehuurl: '/customer/customer/list?filed=2&id=',
        // 选择用户查询条件配置
        selectUserQueryConfig: [
          { key: 'name', label: '客户名称' }
        ],
        MAisMulti:false,//名单选择时 多选还是单选
        MAcallBackUrl:'',//选择名单回调接口
        MAtype:0, //名单参数类型    1===移交 2===负责人 3协同人 4===分配
        MAwidth:1260,//弹框宽度
        description: '退货退款模块管理页面',
        // 表头
        columns: [
          {
            title:'退货退款单编号',
            align:"center",
            dataIndex: 'number',
            scopedSlots: { customRender: 'number' },
          },
          {
            title:'关联客户',
            align:"center",
            dataIndex: 'customerIdDictText',
            scopedSlots: { customRender: 'customerIdDictText' }
          },
          {
            title:'关联合同',
            align:"center",
            dataIndex: 'orderContactsIdDictText',
            scopedSlots: { customRender: 'orderContactsIdDictText' }

          },
          {
            title:'退货退款合同名称',
            align:"center",
            dataIndex: 'name',
            scopedSlots: { customRender: 'name' },
          },
          {
            title:'签订人',
            align:"center",
            dataIndex: 'signedBy_dictText'
          },
          {
            title:'签订日期',
            align:"center",
            dataIndex: 'signedTime',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'负责人',
            align:"center",
            dataIndex: 'directorIds_dictText'
          },
          {
            title:'协同人',
            align:"center",
            dataIndex: 'collaboratorIds_dictText'
          },
          {
            title: '退货产品',
            align: 'center',
            dataIndex: 'productIds_dictText',
            children: [
              {
                title: '产品名称',
                dataIndex: 'chanpinname',
                key: 'chanpinname',
                width: 200,
                align: 'center',
                scopedSlots: { customRender: 'chanpinname' }
              },
              {
                title: '产品编号',
                dataIndex: 'chanpinnumber',
                width: 200,
                align: 'center',
                key: 'chanpinnumber',
                scopedSlots: { customRender: 'chanpinnumber' }
              },
              {
                title: '规格',
                dataIndex: 'specifications',
                width: 150,
                align: 'center',
                key: 'specifications',
                scopedSlots: { customRender: 'specifications' }
              },
              {
                title: '退货数量',
                dataIndex: 'num',
                key: 'num',
                width: 150,
                align: 'center',
                scopedSlots: { customRender: 'num' }
              },
              {
                title: '退货单位',
                dataIndex: 'unitText',
                key: 'unitText',
                width: 150,
                align: 'center',
                scopedSlots: { customRender: 'unitText' }
              },
              {
                title: '退货单价',
                dataIndex: 'unitPrice',
                key: 'unitPrice',
                width: 150,
                align: 'center',
                scopedSlots: { customRender: 'unitPrice' }
              },
              {
                title: '单价小计',
                dataIndex: 'quotedPriceTotal',
                key: 'quotedPriceTotal',
                width: 150,
                align: 'center',
                scopedSlots: { customRender: 'quotedPriceTotal' }
              },
              {
                title: '备注',
                dataIndex: 'remarks',
                key: 'remarks',
                width: 150,
                align: 'center',
                scopedSlots: { customRender: 'remarks' }
              }
            ]
          },
          {
            title:'退款金额总计',
            align:"center",
            dataIndex: 'refundAmount'
          },
          {
            title:'退款理由',
            align:"center",
            dataIndex: 'refundReason_dictText'
          },
          {
            title:'退款备注',
            align:"center",
            dataIndex: 'remark'
          },
          {
            title:'创建人',
            align:"center",
            dataIndex: 'createBy'
          },
          {
            title:'创建时间',
            align:"center",
            dataIndex: 'createTime'
          },
          {
            title:'更新时间',
            align:"center",
            dataIndex: 'updateTime'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        seecolumns: [
          {
            title: '产品名称',
            dataIndex: 'name',
            key: 'name',
            width: 200,
            align: 'center',
            scopedSlots: { customRender: 'name' }
          },
          {
            title: '产品编号',
            dataIndex: 'number',
            width: 200,
            align: 'center',
            key: 'number',
            scopedSlots: { customRender: 'number' }
          },
          {
            title: '规格',
            dataIndex: 'specifications',
            width: 150,
            align: 'center',
            key: 'specifications',
            scopedSlots: { customRender: 'specifications' }
          },
          {
            title: '退货数量',
            dataIndex: 'num',
            key: 'num',
            width: 150,
            align: 'center',
            scopedSlots: { customRender: 'num' }
          },
          {
            title: '退货单位',
            dataIndex: 'unitText',
            key: 'unitText',
            width: 150,
            align: 'center',
            scopedSlots: { customRender: 'unitText' }
          },
          {
            title: '退货单价',
            dataIndex: 'unitPrice',
            key: 'unitPrice',
            width: 150,
            align: 'center',
            scopedSlots: { customRender: 'unitPrice' }
          },
          {
            title: '单价小计',
            dataIndex: 'quotedPriceTotal',
            key: 'quotedPriceTotal',
            width: 150,
            align: 'center',
            scopedSlots: { customRender: 'quotedPriceTotal' }
          },
          {
            title: '备注',
            dataIndex: 'remarks',
            key: 'remarks',
            width: 150,
            align: 'center',
            scopedSlots: { customRender: 'remarks' }
          }
        ],
        url: {
          list: "/refundreturn/refundReturn/list",
          customList: '/customer/customer/list',
          contractlist: "/contractorder/contractOrder/list",
          delete: "/refundreturn/refundReturn/delete",
          deleteBatch: "/refundreturn/refundReturn/deleteBatch",
          exportXlsUrl: "/refundreturn/refundReturn/exportXls",
          importExcelUrl: "refundreturn/refundReturn/importExcel",
          updateCollaboratorIds:"/refundreturn/refundReturn/updateCollaboratorIds",
          updateByDirectorIds:"/refundreturn/refundReturn/updateByDirectorIds",
          deleteUser:"/refundreturn/refundReturn/deleteUser",


        },
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
    this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      kehuChange(e) {
        console.log(e)

      },
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'name',text:'报价名称',dictCode:''})
        fieldList.push({type:'string',value:'number',text:'编号',dictCode:''})
        fieldList.push({type:'sel_search',value:'customerId',text:'客户ID',dictTable:"biz_customer", dictText:'name', dictCode:'id'})
        fieldList.push({type:'sel_search',value:'orderContactsIds',text:'合同ID',dictTable:"biz_contract_order", dictText:'name', dictCode:'id'})
        fieldList.push({type:'string',value:'signedBy',text:'签订人',dictCode:''})
        fieldList.push({type:'date',value:'signedTime',text:'签订时间'})
        fieldList.push({type:'sel_search',value:'directorIds',text:'负责人IDS',dictTable:"sys_user", dictText:'username', dictCode:'id'})
        fieldList.push({type:'sel_search',value:'collaboratorIds',text:'协同人IDS',dictTable:"sys_user", dictText:'username', dictCode:'id'})
        fieldList.push({type:'sel_search',value:'productIds',text:'产品IDS',dictTable:"biz_product", dictText:'name', dictCode:'id'})
        fieldList.push({type:'BigDecimal',value:'refundAmount',text:'退款金额',dictCode:''})
        fieldList.push({type:'sel_search',value:'refundReason',text:'退款理由(1.产品损坏 2.质量问题 3.发错产品  4.其他原因)',dictTable:"", dictText:'', dictCode:'refund_reason'})
        fieldList.push({type:'string',value:'remark',text:'备注',dictCode:''})
        fieldList.push({type:'date',value:'createTime',text:'创建时间'})
        fieldList.push({type:'int',value:'isDelete',text:'是否删除',dictCode:''})
        this.superFieldList = fieldList
      },
      MAction(MAisMulti,MAcallBackUrl,MAtype,MAwidth){
        //Member action
        //设置多选还是单选
        // this.MAisMulti = false
        this.MAisMulti = MAisMulti
        //设置回调接口
        // this.MAcallBackUrl  = '/quotation/quotation/updateDirector'
        this.MAcallBackUrl  = MAcallBackUrl
        //设置回调接口参数
        // this.MAtype = 1
        this.MAtype = MAtype
        //弹框宽度
        // this.MAwidth = 1260
        this.MAwidth = MAwidth
        if (this.selectedRowKeys.length <= 0) {
          this.$message.warning('请至少或者最多选择一条记录！');
          return;
        }
        this.$refs.MAmodel.showModal()
      },
      MACallback(e){
        this.loading = true
        console.log('e',e)
        console.log('this.selectedRowKeys',this.selectedRowKeys)
        let tempArr = e;
        let idArr = tempArr.map(e => {
          return e.id
        })
        let collaboratorIds_idArr = idArr.join(',')
        let ids = this.selectedRowKeys.join(',')
        let obj = {}
        switch (this.MAtype) {
          case 1://移交
            obj = {id:ids,directorIds:e[0].id}
            break;
          case 2://负责人
            obj = {id:ids,directorIds:e[0].id}
            break;
          case 3://协同人
            obj = {id:ids,collaboratorIds:collaboratorIds_idArr}
            break;
          case 4://分配
            obj = {id:ids,directorIds:e[0].id}
            break;

        }
        postAction(this.MAcallBackUrl,obj).then(res=>{
          console.log('移交后',res)
          if(res.success){
            this.selectedRowKeys = []
            this.$message.success(res.message)
            this.loading = false
            this.loadData()
          }else {
            this.$message.error(res.message)
            this.loading = false
            // this.loadData()
          }

        })
      },
      //查看客户详情
      seeCustomerId(record) {
        this.loading = true
        getAction(this.url.customList, { id: record.customerId }).then(res => {
          if (res.success) {
            this.$refs.CustomerModal.dongtai(record.customerId)
            this.$refs.CustomerModal.dongtaiShow = true
            this.$refs.CustomerModal.edit(res.result.records[0])
            this.$refs.CustomerModal.disableSubmit = true
          } else {
            this.$message.error(res.message)
          }
        }).finally(() => {
          this.loading = false
        })
      },
      //查看合同详情
      seeAgreementId(record) {
        this.loading = true
        getAction(this.url.contractlist, { id: record.orderContractId }).then(res => {
          if (res.success) {
            this.$refs.ContractOrderModal.edit(res.result.records[0])
            this.$refs.ContractOrderModal.disableSubmit = true
          } else {
            this.$message.error(res.message)
          }
        }).finally(() => {
          this.loading = false
        })
      },
      //tab页查询
      tabsChange(e) {
        this.tabIndex = e
        console.log(e)
        if (e == 0) {
          this.queryParam = {}
          this.queryParam.filed = this.filed
          this.loadData()
        } else {
          this.queryParam.statusTop = e
          this.loadData()
        }
      },

    }
  }
</script>
<style scoped lang="less">
@import '~@assets/less/common.less';

.buttonStyle {

  button {
    margin-left: 10px;
  }
}

.flex-box {
  display: flex;
  flex-direction: row;
  align-items: center;
}

.ant-table table {
  table-layout: fixed;
}

/*默认表格高度 撑起空白*/
/deep/ .minH .ant-table-body {
  min-height: 400px;
  //border-left: 1px solid #e8e8e8;
  //border-right: 1px solid #e8e8e8;
}

/deep/ .minH .ant-table-placeholder {
  display: none;
}

.ant-table table {
  table-layout: fixed;
}

.table-warp {
  width: 100%;
  word-break: break-all;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1; /* 这里是超出几行省略 */
  overflow: hidden;
  position: relative;
}

/deep/ .ant-popover {
  z-index: 800 !important;
}

.chanpin-table {
  width: 800px;
}

/deep/ .ant-table-tbody > tr:hover:not(.ant-table-expanded-row):not(.ant-table-row-selected) > td {
  .chakanquanbu {
    opacity: 1;
  }
}

.chakanquanbu {
  transition: all 0.3s ease-out;
  opacity: 0;
  position: absolute;
  right: -16px;
  top: -4px;
  background: #e6f7ff;
}

.flex-liangtou {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}

.w100 {
  width: 100%;
}
</style>